package agent;

import processing.core.PVector;

public class LimitVelocityBehavior extends Behavior {

	float maxVelocity;
	
	LimitVelocityBehavior(Main p, float maxVel) {
		super(p);
		maxVelocity = maxVel;
	}
	
	public void apply(Agent a) {
		super.apply(a);
		limitVelocity(a);
	}
	
	public void limitVelocity(Agent a) {
		PVector velocity = a.getCurrentVelocity();
		if (velocity.mag() > maxVelocity) {
			velocity.div(velocity.mag());
			velocity.mult(maxVelocity);
		}
		a.setVelocity(velocity);
	}
	
	public float getMaxVelocity() {
		return maxVelocity;
	}
	
	public void setMaxVelocity(float v) {
		maxVelocity = v;
	}

}
